package tgc.edu.yzy.web.administrator.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.DatagridUtils;
import tgc.edu.yzy.entity.BmsRole;
import tgc.edu.yzy.entity.Dapa;
import tgc.edu.yzy.entity.User;
import tgc.edu.yzy.service.DapaService;
import tgc.edu.yzy.service.RoleService;
import tgc.edu.yzy.service.UserService;
import tgc.edu.yzy.web.administrator.form.Userform; 

@Controller
@RequestMapping(value="administrator/user")
public class AdminUserController {
	@Autowired
	private UserService userservice;
	@Autowired
	private RoleService roleservice;
	@Autowired
	private DapaService dapaservice;
	@RequestMapping(value="index")
	private String index() {
		return "administrator/user/index";
	}
	
	@RequestMapping(value="pagelist")
	@ResponseBody
	private Object pagelist(DatagridUtils form,User user,String sousuo) {
		Pageable pageable=form.buildPageable();
		Page<User> users=null;
		Specification<User> spec = new Specification<User>() {

			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(user.getName())) {
					rules.add(criteriaBuilder.like(root.get("name"), "%"+user.getName()+"%"));
				}else if(StringUtils.hasText(user.getMynumber())) {
					rules.add(criteriaBuilder.like(root.get("mynumber"), "%"+user.getMynumber()+"%"));
				}else if(StringUtils.hasText(sousuo)) {
					rules.add(criteriaBuilder.equal(root.get("dapa").get("id"), sousuo));
				}
				
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		users= userservice.findAll(spec,pageable);
		HashMap<String , Object> result = form.getPageResult(users);
		return result; 
	}
	/***
	 * 重置密码
	 * 系统管理员进行操作
	 * @param id
	 * @return
	 */
	@RequestMapping(value="pwd")
	@ResponseBody
	private String pwd(Integer id) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		User users = userservice.findById(id);
		users.setPassword(encoder.encode("111111"));
		userservice.save(users);
		return "ok";
	}
	/***
	 * 修改权限
	 * @param id
	 * @param code
	 * @return
	 */
	@RequestMapping(value="code")
	@ResponseBody
	private String code(Integer id,String code) {
		User users=userservice.findById(id);
		BmsRole roles=null;
		String mun = null;
		if("ROLE_USER".equals(code)) {
			roles=roleservice.findById(2);
			mun="图书管理员";
		}else if("ROLE_BOOK".equals(code)) {
			roles=roleservice.findById(3);
			mun="用户";
		}
		users.getBmsRole().clear();
		users.getBmsRole().add(roles);
		userservice.save(users);
		return mun;
	}
	/***
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="delete")
	@ResponseBody
	private String delete(Integer id) {
		userservice.deleteByID(id);
		return "ok";
	}
	/***
	 * 保存 或者修改
	 * @param form
	 * @return
	 */
	@RequestMapping(value="save")
	@ResponseBody
	private String save(Userform form,String dapa) {
		User users=null;
		if(form.getId()!=null) {
			users=userservice.findById(form.getId());
			if(!users.getUsername().equals(form.getUsername())) {
				User u=userservice.findByUsername(form.getUsername());
				if(u==null) {
					Integer da=null;
					try {
						da=Integer.parseInt(dapa);
					}catch (Exception e) {
						da=null;
					}
					BeanUtils.copyProperties(form, users);
					if(da!=null) {
						users.setDapa(dapaservice.findById(da));
					}
					userservice.save(users);
				}else {
					return "username";
				}
			}else {
				Integer da=null;
				try {
					da=Integer.parseInt(dapa);
				}catch (Exception e) {
					da=null;
				}
				if(da!=null) {
					users.setDapa(dapaservice.findById(da));
				}
				BeanUtils.copyProperties(form, users);
				userservice.save(users);
			}
		}else {
			 users=new User();
			 User u=userservice.findByUsername(form.getUsername());
				if(u==null) {
					BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
					 BeanUtils.copyProperties(form, users);
					 users.setNumber(0);
					 users.setPassword(encoder.encode("111111"));
					 BmsRole roles = roleservice.findById(3);
					 System.out.println(roles.getName());
					 users.getBmsRole().add(roles);
					 Integer dapas=Integer.parseInt(dapa);
					 Dapa da = dapaservice.findById(dapas);
					 users.setDapa(da);
					 userservice.save(users);
				}else {
					return "username";
				}
		}
		return "ok";
	}
}
